#include "snake.h"
#include "stdlib.h"
#include <stdio.h>

void snakeInit(Snake * pSnake)
{
    ElemListe tete;
    elemSetXY(&tete , 1 ,3);
    pSnake->longeur=3;
    pSnake->deplacementEnCour = I;
    pSnake->queu = (Liste*)malloc(sizeof(Liste));
    initialise(pSnake->queu);
    ajoutEnTete(tete,pSnake->queu);
    elemSetXY(&tete , 2,3);
    ajoutEnTete(tete,pSnake->queu);
    elemSetXY(&tete ,  3,3);
    ajoutEnTete(tete,pSnake->queu);
    elemSetXY(&tete , 4 ,3);
    ajoutEnTete(tete,pSnake->queu);
}

void snakeLibere(Snake *pSnake)
{
    videListe(pSnake->queu);
    free(pSnake->queu);
    pSnake->queu = NULL;
}
void snakeDeplace(Snake *pSnake, Arene *pArene, ElemListe futurPosition)
{
    if(testPositionPills(pArene,elemGetX(futurPosition), elemGetY(futurPosition)))
    {
        snakeMangerPills(pArene, pSnake, futurPosition);
    }
    if(testPositionPerso(pArene , elemGetX(futurPosition), elemGetY(futurPosition)) && !test1ErElementDansListe(pSnake->queu))
    {
       deplaceElementListe(pSnake->queu);
       snakeSetXY(pSnake, elemGetX(futurPosition), elemGetY(futurPosition));
    }
    else
    {
        pSnake->deplacementEnCour =I;
    }
}

void snakeGauche(Snake *pSnake,  Arene* pArene)
{
    ElemListe futurPosition;
    elemSetXY(&futurPosition, snakeGetX(pSnake)-1, snakeGetY(pSnake));
    if(elemGetX(futurPosition) < 0)
        elemSetX(&futurPosition, getDimX(pArene)-1);
    snakeDeplace(pSnake,pArene, futurPosition);
}

/*deplace le serpent vers la droite */
void snakeDroite(Snake *pSnake,  Arene* pArene)

{
    ElemListe futurPosition;
    elemSetXY(&futurPosition, snakeGetX(pSnake)+1,snakeGetY(pSnake));
    if(elemGetX(futurPosition) > getDimX(pArene)-1)
         elemSetX(&futurPosition, 0);
    snakeDeplace(pSnake, pArene,  futurPosition);
}

/*deplace le serpent vers le haut*/
void snakeHaut(Snake *pSnake,  Arene* pArene)

{
    ElemListe futurPosition;
    elemSetXY(&futurPosition, snakeGetX(pSnake),snakeGetY(pSnake)-1);
    if(elemGetY(futurPosition) < 0)
        elemSetY(&futurPosition,getDimY(pArene)-1);
    snakeDeplace(pSnake, pArene, futurPosition);

}

/*deplace le serpent vers le bas */
void snakeBas(Snake *pSnake,  Arene* pArene)
{
    ElemListe futurPosition;
    elemSetXY(&futurPosition, snakeGetX(pSnake),snakeGetY(pSnake)+1);
    if(elemGetY(futurPosition) > getDimY(pArene)-1)
        elemSetY(&futurPosition,0);
    snakeDeplace(pSnake, pArene, futurPosition);
}

void snakeGaucheDir(Snake *pSnake, const Arene *pArene)
{
    if(pSnake->deplacementEnCour!=D)
        pSnake->deplacementEnCour=G;
}

void snakeDroiteDir(Snake *pSnake, const Arene *pArene)
{
    if(pSnake->deplacementEnCour!=G)
        pSnake->deplacementEnCour=D;
}


void snakeHautDir(Snake *pSnake, const Arene *pArene)
{
    if(pSnake->deplacementEnCour!=B)
        pSnake->deplacementEnCour=H;
}


void snakeBasDir(Snake *pSnake, const Arene *pArene)
{
    if(pSnake->deplacementEnCour!=H)
        pSnake->deplacementEnCour=B;
}

void snakeInactifDir(Snake *pSnake, const Arene *pArene)
{
    pSnake->deplacementEnCour=I;
}


int snakeGetX(const Snake *pSnake)
{
    ElemListe Queu=getPremierElemListe(pSnake->queu);
    return elemGetX(Queu);
}

int snakeGetY(const Snake *pSnake)
{
    ElemListe Queu=getPremierElemListe(pSnake->queu);
    return elemGetY(Queu);
}

void snakeSetX(Snake *pSnake,int x)
{
    ElemListe Queu=getPremierElemListe(pSnake->queu);
    elemSetX(&Queu, x);
    setPremierElemListe(pSnake->queu, Queu);
}

void snakeSetY(Snake *pSnake,int y)
{
    ElemListe Queu=getPremierElemListe(pSnake->queu);
    elemSetY(&Queu, y);
    setPremierElemListe(pSnake->queu, Queu);
}

void snakeSetXY(Snake *pSnake,int x, int y)
{
    ElemListe e;
    elemSetXY(&e,x,y);
    setPremierElemListe(pSnake->queu, e);
}

/*
int snakeTestQueu(const Snake *pSnake, int x, int y)
{
    return testElementDansListe(GetPremierElemListe, pSnake->queu);
}*/

void snakeMangerPills(Arene *pArene, Snake *pSnake, ElemListe ajout)
{
    areneSetXY(pArene,elemGetX(ajout),elemGetY(ajout),'.');
    ajoutEnQueue(ajout,pSnake->queu);
    setPills(pArene,pSnake);
    pSnake->longeur++;
}

void setPills(Arene *pArene, Snake *pSnake)
{
    ElemListe test;
    int x,y;
    do
    {
        x=(rand()%(getDimX(pArene)-2)+1);
        y=(rand()%(getDimY(pArene)-2)+1);
        elemSetXY(&test, x ,y);
    }
    while(testElementDansListe(pSnake->queu, test));
    areneSetXY(pArene,elemGetX(test), elemGetY(test),'*');
}
